// loggers/loggers.go
package loggers

import (
	"log"
	"sync"

	"gitee.com/chuanjinge/go-container/pkg/dirfile"
)

var (
	loggersConf  *LoggersConf
	onceinitConf sync.Once
)

type LoggersConf struct {
	LoggerConf   *log.Logger // LoggerConf 使用覆盖模式
	ConfFilePath string
}

func InitializeLoggerConf(ConfFilePath string) error {
	var err error
	onceinitConf.Do(func() {
		if err := dirfile.EnsureDirExists(ConfFilePath); err != nil {
			return
		}

		// 初始化 LoggerConf（覆盖模式）。
		fileConf, err := NewOverwriteWriter(ConfFilePath)
		if err != nil {
			return
		}
		loggerConf := log.New(fileConf, "", 0)

		loggersConf = &LoggersConf{
			LoggerConf:   loggerConf,
			ConfFilePath: ConfFilePath,
		}
	})
	return err
}

func GetLogConf() *LoggersConf {
	return loggersConf
}

func CloseLoggerConf() {
	if loggersConf != nil {
		loggersConf.LoggerConf.SetOutput(nil)
	}
}
